package com.sam.designpattern.mediator;

/**
 * @Author: Sam
 * @Date: 2022-08-02 11:24
 * @Description: 主板类，实现中介者接口
 */
public class MotherBoard implements Mediator {

    /**
     * 持有并维护同事类-光驱类
     */
    public CDDriver cdDriver;

    /**
     * 持有并维护同事类-CPU类
     */
    public CPU cpu;

    /**
     * 持有并维护同事类-显卡类
     */
    public VideoCard videoCard;

    /**
     * 持有并维护同事类-声卡类
     */
    public SoundCard soundCard;

    @Override
    public void changed(Colleague colleague) {
        if (colleague == cdDriver) {
            //表示光驱读取数据了
            this.opeCDDriverReadData((CDDriver) colleague);
        } else if (colleague == cpu) {
            //表示CPU处理完了
            this.opeCPU((CPU) colleague);
        }
    }

    private void opeCDDriverReadData(CDDriver cd) {
        //1：先获取光驱读取的数据
        String data = cd.getData();
        //2：把这些数据传递给CPU进行处理
        this.cpu.executeData(data);
    }

    private void opeCPU(CPU cpu) {
        //1：先获取CPU处理后的数据
        String videoData = cpu.getVideoData();
        String soundData = cpu.getSoundData();
        //2：把这些数据传递给显卡和声卡展示出来
        this.videoCard.showData(videoData);
        this.soundCard.soundData(soundData);
    }


    public void setCdDriver(CDDriver cdDriver) {
        this.cdDriver = cdDriver;
    }

    public void setCpu(CPU cpu) {
        this.cpu = cpu;
    }

    public void setVideoCard(VideoCard videoCard) {
        this.videoCard = videoCard;
    }

    public void setSoundCard(SoundCard soundCard) {
        this.soundCard = soundCard;
    }
}
